package cn.ym.mall.service.impl;

import cn.ym.mall.dao.UserDao;
import cn.ym.mall.dao.impl.UserDaoImpl;
import cn.ym.mall.entity.User;
import cn.ym.mall.service.UserService;
import cn.ym.mall.utils.DruidUtils;
import cn.ym.mall.vo.Page;

import java.sql.Connection;
import java.sql.SQLException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();
    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @Override
    public boolean addUser(User user) {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if(userDao.addUser(user) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 查看个人信息
     *
     * @param uid
     * @return
     */
    @Override
    public User getUserByUid(Integer uid) {
        Connection conn = null;
        User user = null;
        try {
            conn = DruidUtils.getConnection();
            user = userDao.getUserByUid(uid);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return user;
    }

    /**
     * 修改个人信息
     *
     * @param user
     * @return
     */
    @Override
    public boolean updateUser(User user) {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if(userDao.updateUser(user) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 查询用户信息
     *
     * @return
     */
    @Override
    public List<User> getAllUser() {
        Connection conn = null;
        List<User> userList = new ArrayList<>();
        try {
            conn = DruidUtils.getConnection();
            userList = userDao.getAllUser();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return userList;
    }

    /**
     * 查询用户状态
     *
     * @param uid
     * @return
     */
    @Override
    public User getUserByustate(Integer uid) {
        Connection conn = null;
        User user = null;
        try {
            conn = DruidUtils.getConnection();
            user = userDao.getUserByustate(uid);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return null;
    }

    /**
     * 分页查询用户
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Page<User> getUserPage(int pageNo, int pageSize) {
        Connection conn = null;
        Page<User> page = new Page<User>(pageNo,pageSize);
        try {
            conn = DruidUtils.getConnection();
            page.setCount(userDao.getUserCount());
            page.setList(userDao.getUserPage(pageNo,pageSize));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return page;
    }

    /**
     * 通过uid删除用户
     *
     * @param uid
     * @return
     */
    @Override
    public boolean deleteUser(Integer uid) {
        Connection conn = null;
        try {
            conn = DruidUtils.getConnection(false);
            if(userDao.deleteUser(uid) == 1){
                DruidUtils.commit();
                return true;
            }else {
                DruidUtils.rollback();
            }
        }catch (Exception e){
            DruidUtils.rollback();
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return false;
    }

    /**
     * 登录
     *
     * @param uname
     * @return
     */
    @Override
    public User login(String uname) {
        Connection conn = null;
        User user = null;
        try {
            conn = DruidUtils.getConnection();
            user = userDao.getUserByName(uname);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            DruidUtils.close();
        }
        return user;
    }

}
